﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Printing;
using DeepEarth.Client.Controls.DEMap;
using DeepEarth.Client.MapControl;

namespace ExampleControl.Popups
{
    public partial class PrintPreviewDialog
    {
        public PrintPreviewDialog(Map map)
        {
            InitializeComponent();
            MapInstance = map;
        }

        private void OkButtonClick(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void ChildWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (DialogResult == true)
            {
                // create an instance of PrintDocument
                var document = new PrintDocument();

                // tell the API what to print
                document.PrintPage += (s, args) =>
                {
                    //args.PageVisual = this.previewImage;
                    args.PageVisual = new Image { Source = new WriteableBitmap(MapInstance.BaseLayer.Msi, null) };
                };

                // call the Print() with a proper name which will be visible in the Print Out
                document.Print("Silverlight Print Application Demo");
            }
        }

        public Map MapInstance { get; set; }
    }
} 